package com.zxh.utils;

import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CookieEncrypTool {
	/**
	 * Base64加密
	 * @param cleartext
	 * @return
	 */
	public static String encodeBase64(String  cleartext){
		String resultText="";
		BASE64Encoder encoder = new BASE64Encoder();
		try {
			resultText = encoder.encode(cleartext.getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultText;
	}
	/**
	 * Base64解密
	 * @param cleartext
	 * @return
	 */
	public static String decodeBase64(String ciphertext){
		String resultText="";
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			resultText = new String(decoder.decodeBuffer(ciphertext),"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultText;
	}
	//测试加密、解密
	public static void main(String[] args) {
		String str="你ddd额555";
		String rs=encodeBase64(str);
		System.out.println(rs);
		String str1="5L2gZGRk6aKdNTU1";
		System.out.println(decodeBase64(str1));
	}
}
